//--------------------------------------------------------------------------
// $Id: Association.java 2 2006-08-20 04:04:26Z zendragon $
//--------------------------------------------------------------------------
// Copyright 2006 Social Brain Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//--------------------------------------------------------------------------

package org.socialbrain.openid;

import org.apache.commons.lang.StringUtils;

import java.math.BigInteger;

/**
 * @author <a href="http://erning.net/">Zhang Erning</a>
 * @version $Id: Association.java 2 2006-08-20 04:04:26Z zendragon $
 */
public class Association extends KeyValuePairs
{
    public Association()
    {
    }

    public Association(KeyValuePairs pairs)
    {
        super(pairs);
    }

    // commons

    public void setAssocType(String value)
    {
        put(Constants.PARAM_ASSOC_TYPE, value);
    }

    public String getAssocType()
    {
        return get(Constants.PARAM_ASSOC_TYPE);
    }

    public void setAssocHandle(String value)
    {
        put(Constants.PARAM_ASSOC_HANDLE, value);
    }

    public String getAssocHandle()
    {
        return get(Constants.PARAM_ASSOC_HANDLE);
    }

    public void setExpireIn(int expireIn)
    {
        putAsInt(Constants.PARAM_EXPIRES_IN, expireIn);
    }

    public int getExpireIn()
    {
        return getAsInt(Constants.PARAM_EXPIRES_IN);
    }

    public void setSessionType(String value)
    {
        put(Constants.PARAM_SESSION_TYPE, value);
    }

    public String getSessionType()
    {
        return get(Constants.PARAM_SESSION_TYPE);
    }

    // Unencrypted

    public void setMacKey(byte[] value)
    {
        putAsByteArray(Constants.PARAM_MAC_KEY, value);
    }

    public byte[] getMacKey()
    {
        return getAsByteArray(Constants.PARAM_MAC_KEY);
    }

    public boolean isUnencrypted()
    {
        return StringUtils.isEmpty(getSessionType())
                || Constants.SESSION_NO_ENCRYPTION.equals(getSessionType());
    }

    // Diffie-Hellman

    public void setDhServerPublic(BigInteger value)
    {
        putAsBigInteger(Constants.PARAM_DH_SERVER_PUBLIC, value);
    }

    public BigInteger getDhServerPublic()
    {
        return getAsBigInteger(Constants.PARAM_DH_SERVER_PUBLIC);
    }

    public void setEncMacKey(byte[] value)
    {
        putAsByteArray(Constants.PARAM_ENC_MAC_KEY, value);
    }

    public byte[] getEncMacKey()
    {
        return getAsByteArray(Constants.PARAM_ENC_MAC_KEY);
    }

    // Unsuccessful

    public void setError(String value)
    {
        put(Constants.PARAM_ERROR, value);
    }

    public String getError()
    {
        return get(Constants.PARAM_ERROR);
    }

    public void setErrorCode(String value)
    {
        put(Constants.PARAM_ERROR_CODE, value);
    }

    public String getErrorCode()
    {
        return get(Constants.PARAM_ERROR_CODE);
    }

    public boolean isError()
    {
        return getErrorCode() != null || getError() != null;
    }
}
